package org.example.mybatisdemo.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatisdemo.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    /**
     * 结果印射
     * 方法一: 对字段进行重命名
     * @return
     */
    @Select("select id, username, password, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userinfo")
    List<UserInfo> selectAll();

    /**
     * 结果印射
     * 方法二: 对字段进行重命名
     * @return
     */

    @Results(id = "BaseMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectAll2();

    // 复用Results的定义
    @ResultMap("BaseMap")
    @Select("select * from userinfo where id = #{id}")
    UserInfo selectOne(Integer id);

    @Select("select * from userinfo where id = #{userId}")
    UserInfo selectOne1(@Param("userId") Integer id);

    //返回自增id
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, password, age, gender, phone)" +
            " values (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert(UserInfo userInfo);

    //对insert的参数进行重命名
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, password, age, gender, phone)" +
            " values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    //删除操作
    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    //更改操作
    @Update("update userinfo set age = #{age} where id = #{id}")
    Integer update(UserInfo userInfo);
}
